<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getTableData" @reset="() => reset()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="wpbm" class="ml-2">
          <a-input v-model:value="formState.wpbm" class="!w-72" placeholder="药品编号/药品名称/助记码" allow-clear />
        </a-form-item>
        <a-form-item name="stdate" label="日期范围" class="ml-3 mr-3">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="ckbh" label="仓库" class="ml-3 mr-3 w-72">
          <a-select v-model:value="formState.ckbh" class="w-full" placeholder="请选择">
            <a-select-option v-for="(item, index) in storageData" :key="index" :value="item.ckid">{{ item.ckmc }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label=" ">
          <a-radio-group v-model:value="formState.sfcType">
            <a-radio value="">全部</a-radio>
            <a-radio :value="0">卫生材料</a-radio>
            <a-radio :value="1">易耗品</a-radio>
            <a-radio :value="2">固定资产</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3 flex justify-end">
        <BaseExport
          type="药库"
          name="进销存查询"
          code="000408"
          :search-params="{
            stdate: formState.dateRange?.[0],
            eddate: formState.dateRange?.[1],
            wpbm: formState.wpbm,
            ckbh: formState.ckbh
          }"
          >导出</BaseExport
        >
      </div>
      <BaseTable :loading="loading" column-code="000408" :data-source="tableData.listDatile" row-key="id" :extra-height="50" bordered fill>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            <div>{{ index + 1 }}</div>
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record.mc, gg: record.gg, cj: record.cj }" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="openDetail(record)">出入库明细</a-button>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell />
              <a-table-summary-cell>
                <a-typography-text>合计</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell />
              <a-table-summary-cell />
              <a-table-summary-cell class="text-left">
                <a-typography-text>{{ tableData.slQcHj }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(tableData.jeQcHj) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell />
              <a-table-summary-cell />
              <a-table-summary-cell class="text-left">
                <a-typography-text>{{ tableData.slSrHj }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(tableData.jeSrHj) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell />
              <a-table-summary-cell class="text-left">
                <a-typography-text>{{ tableData.slFcHj }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(tableData.jeFcHj) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell />
              <a-table-summary-cell class="text-left">
                <a-typography-text>{{ tableData.slQmHj }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(tableData.jeQmHj) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell />
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
    <!-- <Detail v-model:open="detailOpen" :row-data="rowData" @success="onSuccess" /> -->
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { formatAmount } from '@/utils/format.js'
// import Detail from './Detail'

const formRef = ref(null)
const loading = ref(false)
const tableData = ref({
  listDatile: []
})
const detailOpen = ref(false)
const rowData = ref({})
const formState = ref({
  sfcType: '',
  wpbm: '',
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 20)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  ckbh: undefined
})
const getTableData = async () => {
  loading.value = true
  const { data, code, msg } = await post('/drugStorage/consume/getInvoicingWarehouse', {
    stdate: formState.value.dateRange?.[0],
    eddate: formState.value.dateRange?.[1],
    ...formState.value
  })
  loading.value = false
  if (code !== 0) return message.error(msg)
  tableData.value = data || {}
}
const openDetail = (record) => {
  detailOpen.value = true
  rowData.value = record
  rowData.value.dateRange = formState.value.dateRange
}
const storageData = ref([])
const getThreeStorageData = async () => {
  const { data, code, msg } = await post('/operation/opex/getWareHouseInfoList', {
    cklx: ['CK003']
  })
  if (code !== 0) return message.error(msg)
  storageData.value = data
}
// const onSuccess = () => {
//   getTableData()
// }
const reset = () => {
  formState.value = {
    wpbm: '',
    dateRange: [dayjs(new Date().setDate(new Date().getDate() - 20)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
    ckbh: undefined
  }
  getTableData()
}
onMounted(() => {
  getThreeStorageData()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
